package com.decheng.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Author 孔德成
 * @Slogan 致敬大师，致敬未来的你
 * @date 2021/7/3
 */
@Controller
public class IndexController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "index";
    }


    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(
            @RequestParam String email,
            @RequestParam String password,
            @RequestParam MultipartFile avatar,
            HttpServletRequest request,
            Model model) {
        System.out.println("email = " + email);
        System.out.println("password = " + password);
        System.out.println("文件类型 = " + avatar.getContentType());
        String originalFilename = avatar.getOriginalFilename();
        System.out.println("原始文件名 = " + originalFilename);
        System.out.println("文件大小 = " + avatar.getSize());

        // 创建上传目录
        String realPath = request.getServletContext().getRealPath("/uploads");
        File uploadDir = new File(realPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }

        try {
            // 将图片保存在本地
            avatar.transferTo(new File(realPath + File.separator + originalFilename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 返回展示图片下载视图
        model.addAttribute("imgPath", "/uploads/" + originalFilename);
        model.addAttribute("fileName", originalFilename);
        return "download";
    }

    @GetMapping("/download/{img}")
    public ResponseEntity<byte[]> downloadImg(
            HttpServletRequest request,
            @PathVariable String img) {
        // 1. 获取图片的真实路径
        String realPath = request.getServletContext().getRealPath("/uploads/" + img);
        // 2. 创建输入流
        byte[] bytes = null;
        try (InputStream is = new FileInputStream(realPath)) {
            // 3. 指定字节数组大小
            bytes = new byte[is.available()];
            // 4. 将流读到数组中
            is.read(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 5. 创建 HttpHeaders 设置响应头信息
        MultiValueMap<String, String> headers = new HttpHeaders();
        // 告诉浏览器以附件的形式下载该文件
        headers.add("Content-Disposition", "attachment;filename=" + img);
        // 设置响应状态码
        HttpStatus ok = HttpStatus.OK;
        // 6. 返回 ResponseEntity 对象
        return new ResponseEntity<>(bytes, headers, ok);
    }

}
